package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysScenicSpot;
import com.mars.module.admin.entity.SysTravelRoute;
import com.mars.module.admin.entity.SysTravelRouteDetails;
import com.mars.module.admin.mapper.SysScenicSpotMapper;
import com.mars.module.system.entity.SysUser;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysSpotReviewRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysSpotReviewMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysSpotReview;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysSpotReviewService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 景点评论业务层处理
 *
 * @author mars
 * @date 2024-03-26
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysSpotReviewServiceImpl implements ISysSpotReviewService {

    private final SysSpotReviewMapper baseMapper;
    private final SysUserMapper userMapper;
    private final SysScenicSpotMapper spotMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysSpotReview add(SysSpotReviewRequest request) {
        SysSpotReview entity = SysSpotReview.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysSpotReviewRequest request) {
        SysSpotReview entity = SysSpotReview.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysSpotReview getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysSpotReview> pageList(SysSpotReviewRequest request) {
        Page<SysSpotReview> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysSpotReview> query = this.buildWrapper(request);
        IPage<SysSpotReview> pageRecord = baseMapper.selectPage(page, query);
        List<SysSpotReview> records = pageRecord.getRecords();
        List<SysSpotReview> recordsNew = new ArrayList<>();
        for (SysSpotReview e : records) {
            SysScenicSpot scenicSpot = spotMapper.selectById(e.getSpotId());
            SysUser sysUser = userMapper.selectById(e.getUserId());
            e.setUserName(sysUser == null ? "" : sysUser.getUserName());
            e.setSpotName(scenicSpot == null ? "" : scenicSpot.getName());
            recordsNew.add(e);
        }
        if (StringUtils.isNotBlank(request.getSpotName())) {
            recordsNew = recordsNew.stream().filter(e -> e.getSpotName().contains(request.getSpotName())).collect(Collectors.toList());
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysSpotReview> list(SysSpotReviewRequest request) {
        LambdaQueryWrapper<SysSpotReview> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysSpotReview> buildWrapper(SysSpotReviewRequest param) {
        LambdaQueryWrapper<SysSpotReview> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getContent())) {
            query.like(SysSpotReview::getContent, param.getContent());
        }
        if (param.getSpotId() != null) {
            query.eq(SysSpotReview::getSpotId, param.getSpotId());
        }
        return query;
    }

}
